;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname HOFC-exercises) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) (require rackunit) (require "extras.rkt") ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; DATA DEFINITIONS ;; a Posn is a (make-posn Number Number) ;; TEMPLATE: ;; posn-fn : Posn -> ?? (define (posn-fn p) (... (posn-x p) (posn-y p))) ;; A ListOfPosn is one of: ;; -- empty ;; -- (cons Posn ListOfPosn) ;; An IntPosn is a (make-posn Integer Integer) ;; A ListOfIntPosn is one of ;; -- empty ;; -- (cons IntPosn ListOfIntPosn) ;; A ListOfInt is one of ;; -- empty ;; -- (cons Integer ListOfInt) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PROBLEM #1 ;; posns-moved-by-posn: ListOfPosn Posn -> ListOfPosn ;; GIVEN: a list of positions and a vector delta represented as a Posn ;; RETURNS: the result of adding the vector delta to each posn in the ;; list ;; EXAMPLE: See test below ;; STRATEGY: (begin-for-test (check-equal? (posns-moved-by-posn (list (make-posn 0 0) (make-posn 1 2) (make-posn 3 6)) (make-posn -1 -2)) (list (make-posn -1 -2) (make-posn 0 0) (make-posn 2 4)))) ;; notice these values are chosen to be non-degenerate (ie, no ;; coincidences). ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PROBLEM #2 ;; ;; good-posns : ListOfIntPosn Integer -> ListOfIntPosn ;; GIVEN: a list of integer positions and an integer x0 ;; RETURNS: a list of the positions whose x coordinate is x0 ;; EXAMPLE: see test below ;; STRATEGY: (begin-for-test (check-equal? (good-posns (list (make-posn 10 20) (make-posn 30 40) (make-posn 10 22)) 10) (list (make-posn 10 20) (make-posn 10 22)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PROBLEM #3 ;; ys-of-x0-points : ListOfIntPosn Integer -> ListOfInteger ;; GIVEN: a list of integer positions and an integer x0 ;; RETURNS: a list of the y coordinates of the positions whose x ;; coordinate is x0 ;; EXAMPLE: see test below ;; STRATEGY: (begin-for-test (check-equal? (ys-of-x0-points (list (make-posn 10 20) (make-posn 30 40) (make-posn 10 22)) 10) (list 20 22))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PROBLEM #4 ;; vertical-line : ListOfInt Integer -> ListOfIntPosn ;; GIVEN: A list of integers ys and an integer x ;; RETURNS: A list of Posns whose x coordinates are all x and whose y ;; coordinates are the numbers in ys. ;; EXAMPLE: See test below ;; STRATEGY: (begin-for-test (check-equal? (vertical-line (list 10 30 40) 2) (list (make-posn 2 10) (make-posn 2 30) (make-posn 2 40)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PROBLEM #5 ;; sum-of-multiples : ListOfNumber Number -> Number ;; GIVEN: a list of numbers lon and a number x ;; RETURNS: the sum of v*x for each element v of lon. (begin-for-test (check-equal? (sum-of-multiples (list 2 5 10) 3) (+ 6 15 30))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; PROBLEM #6 ;; insert-separators : ListOfString String -> String ;; GIVEN: a list of strings los and a string sep ;; RETURNS: the string obtained by concatenating all the strings in los ;; with the separator in between ;; EXAMPLES: See tests below ;; STRATEGY: (begin-for-test (check-equal? (insert-separators empty ", ") "") (check-equal? (insert-separators (list "one") ", ") "one") (check-equal? (insert-separators (list "one" "two" "three") ", ") "one, two, three"))